package com.zhongjian.technologycal.mall.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.activity.web.WebViewMarkActivity;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.bean.CodeBean;
import com.zhongjian.technologycal.mall.bean.RegisterUserBean;
import com.zhongjian.technologycal.mall.http.BaseSubscriber;
import com.zhongjian.technologycal.mall.http.ExceptionHandle;
import com.zhongjian.technologycal.mall.http.MyApiService;
import com.zhongjian.technologycal.mall.http.RetrofitClient;
import com.zhongjian.technologycal.mall.utils.Countdown;
import com.zhongjian.technologycal.mall.utils.LoadingDialog;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.StatusBarUtil;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.ValidatorUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.RequestBody;


/**
 * Created by wei on 2017/11/6.
 * 注册页面的实现
 */

public class RegisterActivity extends BaseActivity {
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.register_et_user_phone)
    EditText register_et_user_phone;
    @BindView(R.id.register_et_password)
    EditText register_et_password;
    @BindView(R.id.register_et_code)
    EditText register_et_code;
    @BindView(R.id.register_et_invite_code)
    EditText register_et_invite_code;
    @BindView(R.id.register_tv_send_code)
    TextView register_tv_send_code;
    @BindView(R.id.register_tv_user_agreement)
    TextView register_tv_user_agreement;
    @BindView(R.id.checkBox_cb)
    CheckBox checkBox_cb;

    @Override
    protected void beforeInit() {
        super.beforeInit();
//        StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.border_color));
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_main_title.setText("注册");
    }

    @Override
    protected void initViewListener() {
        super.initViewListener();
    }

    @Override
    public void initPresenter() {

    }

    /**
     * 发送验证码
     */
    @OnClick(R.id.register_tv_send_code)
    public void onClickRegisterSendCode() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;

        } else {

            if (TextUtils.isEmpty(register_et_user_phone.getText().toString().trim())) {
                ToastUitl.showShort("手机号不能为空～");
                return;
            } else if (!ValidatorUtils.isMobile(register_et_user_phone.getText().toString().trim())) {
                ToastUitl.showShort("手机号格式不正确～");
                return;
            } else {
                new Countdown(register_tv_send_code, 60, "秒后重试", false);
                register_tv_send_code.setClickable(false);
                MyApiService service = RetrofitClient.getInstance(RegisterActivity.this).create(MyApiService.class);
                Map<String, String> maps = new HashMap<String, String>();
                maps.put("phoneNum", register_et_user_phone.getText().toString().trim());
                RequestBody body = RequestBody.create(
                        okhttp3.MediaType.parse("application/json; charset=utf-8"), new Gson().toJson(maps));

                // execute and add observable to RxJava
                RetrofitClient.getInstance(RegisterActivity.this).createBaseApi().execute(
                        service.jsonSendCode(body), new BaseSubscriber<CodeBean>(RegisterActivity.this) {
                            @Override
                            public void onStart() {
                                super.onStart();
                                LoadingDialog.showDialogForLoading(RegisterActivity.this);
                            }

                            @Override
                            public void onError(ExceptionHandle.ResponeThrowable e) {
                                Log.e("Lyk", e.getMessage());

                            }

                            @Override
                            public void onNext(CodeBean codeBean) {
                                if (codeBean.getError_code() == 0) {
                                    ToastUitl.showShort(codeBean.getError_message());

                                } else {
                                    new Countdown(register_tv_send_code, 0, "秒后重试", false);
                                    register_tv_send_code.setClickable(true);
                                    ToastUitl.showShort(codeBean.getError_message());
                                }


                            }

                            @Override
                            public void onCompleted() {
                                super.onCompleted();
                                LoadingDialog.cancelDialogForLoading();
                            }
                        });
            }

        }


    }

    /**
     * 提交注册用户
     */
    @OnClick(R.id.register_tv_commit_reg)
    public void onClickRegisterUser() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;

        } else {
            if (TextUtils.isEmpty(register_et_user_phone.getText().toString().trim())) {
                ToastUitl.showShort("手机号不能为空～");
                return;
            } else if (!ValidatorUtils.isMobile(register_et_user_phone.getText().toString().trim())) {
                ToastUitl.showShort("手机号格式不正确～");
                return;
            } else if (TextUtils.isEmpty(register_et_password.getText().toString().trim())) {
                ToastUitl.showShort("密码不能为空～");
                return;
            } else if (TextUtils.isEmpty(register_et_code.getText().toString().trim())) {
                ToastUitl.showShort("验证码不能为空～");
                return;
            } else if(!checkBox_cb.isChecked()){
                ToastUitl.showShort("请先点选注册协议～");
                return;
            }

            else {
                Map<String, String> maps = new HashMap<String, String>();
                if (TextUtils.isEmpty(register_et_invite_code.getText().toString().trim())) {
                    maps.put("phoneNum", register_et_user_phone.getText().toString().trim());
                    maps.put("password", register_et_password.getText().toString().trim());
                    maps.put("verifyCode", register_et_code.getText().toString().trim());
                } else {
                    maps.put("phoneNum", register_et_user_phone.getText().toString().trim());
                    maps.put("password", register_et_password.getText().toString().trim());
                    maps.put("verifyCode", register_et_code.getText().toString().trim());
                    maps.put("inviteCode", register_et_invite_code.getText().toString().trim());
                }
                MyApiService service = RetrofitClient.getInstance(RegisterActivity.this).create(MyApiService.class);
                RequestBody body = RequestBody.create(
                        okhttp3.MediaType.parse("application/json; charset=utf-8"), new Gson().toJson(maps));

                // execute and add observable to RxJava
                RetrofitClient.getInstance(RegisterActivity.this).createBaseApi().execute(
                        service.jsonRegisterUser(body), new BaseSubscriber<RegisterUserBean>(RegisterActivity.this) {
                            @Override
                            public void onStart() {
                                super.onStart();
                                LoadingDialog.showDialogForLoading(RegisterActivity.this);
                            }

                            @Override
                            public void onError(ExceptionHandle.ResponeThrowable e) {
                                Log.e("Lyk", e.getMessage());

                            }

                            @Override
                            public void onNext(RegisterUserBean registerUserBean) {
                                LoadingDialog.cancelDialogForLoading();
                                try {
                                   if(registerUserBean.getError_code() == 0){
                                       ToastUitl.showShort(registerUserBean.getError_message());
                                       finish();
                                   }else{
                                       ToastUitl.showShort(registerUserBean.getError_message());
                                   }

                                }catch (Exception e){
                                    e.printStackTrace();
                                }

                            }

                            @Override
                            public void onCompleted() {
                                super.onCompleted();
                               LoadingDialog.cancelDialogForLoading();
                            }
                        });


            }


        }

    }

    @OnClick(R.id.iv_back_page)
    public void onClickBack() {
        finish();
    }

    @OnClick(R.id.register_tv_user_agreement)
    public void onClickUserAgreement(){
        Intent intent = new Intent();
        intent.setClass(getBaseContext(), WebViewMarkActivity.class);
        intent.putExtra("url", BaseContacts.URL_REGITSTER_AGREEMENT);
        startActivity(intent);
    }

}
